package timgmbh.monopoly.general;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.util.ArrayList;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;

import org.apache.catalina.websocket.MessageInbound;
import org.apache.catalina.websocket.StreamInbound;
import org.apache.catalina.websocket.WebSocketServlet;
import org.apache.catalina.websocket.WsOutbound;

@WebServlet(name =  "WebSocket", urlPatterns = {"/websocket"})
public class Websocket extends WebSocketServlet {
    private static final long serialVersionUID = 1L;
 
    private ArrayList<Player> players;
    
    public Websocket() {
    	players = new ArrayList<Player>();
    }
    
    public StreamInbound createWebSocketInbound(String protocol, HttpServletRequest request){
        return new MyMessageInbound();
    }
 
    private class MyMessageInbound extends MessageInbound {
        WsOutbound myoutbound;
 
        @Override
        public void onOpen(WsOutbound outbound){
            this.myoutbound = outbound;
            
            players.add(new Player(this));
        }
 
        @Override
        public void onClose(int status){
            System.out.println("Close Client.");
            players.remove(getPlayerByMmid(this));
        }
 
        @Override
        public void onTextMessage(CharBuffer cb) throws IOException{
            System.out.println("Accept Message : "+ cb);
            
            String identifier;
            String value;
            
            try {
	            identifier = cb.toString().substring(0,cb.toString().indexOf(':')).toUpperCase();
	            value = cb.toString().substring(cb.toString().indexOf(':') + 1, cb.length());
	            System.out.println("Identifier: " + identifier + ", " + value);
            } catch (Exception e) {
            	identifier = cb.toString();
            	value = "";
            }
            
            if (identifier.equals("ID")) {
            	for (Player p: players) {
                	if (p.getMmib() == this) p.setId(Integer.valueOf(value));
                }
            } else if (identifier.equals("RELOAD")) {
            	for (Player p: players) {
            		if (p.getId() != getPlayerByMmid(this).getId()) p.getMmib().myoutbound.writeTextMessage(CharBuffer.wrap("RELOAD"));
            	}
            }
        }
 
        @Override
        public void onBinaryMessage(ByteBuffer bb) throws IOException{
        	System.out.println("Binary Message received.");
        }
    }
    
    private class Player {
    	private MyMessageInbound mmib;
    	private int id;
    	
    	public Player(MyMessageInbound mmib) {
    		this.mmib = mmib;
    	}
    	public void setId(int id) {
    		this.id = id;
    	}
    	public MyMessageInbound getMmib() {
    		return mmib;
    	}
    	public int getId() {
    		return id;
    	}
    }
    
    private Player getPlayerByMmid(MyMessageInbound mmib) {
    	for (Player p: players) {
        	if (p.getMmib() == mmib) return p;
        }
    	return null;
    }
}
